package red.stu.pconstraint.util;

import java.lang.reflect.Method;

/**
 * @author yihrmc @stu.red
 * @date 2019/11/21 16:54
 */
public class MethodUtils {

    /**
     * 方法转字符串
     * @param method
     * @return
     */
    public static String toString(Method method) {
        if (method == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder()
                .append(method.getReturnType().getName())
                .append(" ")
                .append(method.getDeclaringClass().getName())
                .append("#")
                .append(method.getName());
        Class<?>[] types = method.getParameterTypes();
        stringBuilder.append("(");
        if (types.length > 0) {
            stringBuilder.append(types[0].getName());
            for (int i = 1; i < types.length; i++) {
                stringBuilder.append(",")
                        .append(types[i].getName());
            }
        }
        stringBuilder.append(")");
        return stringBuilder.toString();
    }

}
